const path = require("path");
const log4js = require("log4js");
log4js.configure({
  //输出配置项：appenders
  appenders: {
    default: {
      type: "stdout",
    },
    sqlOut: {
      //定义出口名
      type: "dateFile", //备份的时候会加上日期
      filename: path.resolve(__dirname, "./out/sql.txt"),
      daysToKeep: 10, //保留x个备份文件
      maxLogSize: 1024 * 1024, //配置文件的最大字节数
      layout: {
        type: "pattern",
        pattern: "%c [%d{yyyy-MM-dd hh:mm:ss}] [%p]: %m%n",
      },
    },
  },
  //定义分类
  categories: {
    default: {
      //必须要有默认分类
      appenders: ["default"],
      level: "all",
    },
    sql: {
      //分类名
      appenders: ["sqlOut"], //该分类的出口
      level: "all", //日志等级
    },
  },
});

//当node程序退出前执行写完所有
process.on("exit", () => {
  log4js.shutdown();
});

//得到分类名为sql的日志对象
const sqlLogger = log4js.getLogger("sql");
exports.sqlLogger = sqlLogger; //导出
